<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="cascade"></div>
	</body>
</html>
<script>
	//object beget
	if(typeof Object.beGet !== "function"){
		Object.beGet = function(o){
			var F = function(){};
			F.prototype = o;
			return new F();
		};
	}
	var stroge = function(){
		var i={};
		return {
			name1:"q"
		}
	};
	var stroge2 = new Function();
	var stroge1 = stroge();
	var auother = Object.beGet(stroge1);
	console.log(stroge.name1);
	console.log(auother.name1);
	
	var que = function(string){
		this.status = string; 
	}
	
	que.prototype.getValue = function(){
		return this.status;
	}
	
	var elem = new que("name");
	console.log(elem.getValue());
	
	//关于异常
	
	function add(a,b){
		if(typeof a !== "number" || typeof b !== "number"){
			throw{
				name:"TypeErro",
				message:"add neeeds number"
			}
		}else{
			var total = 0;
			for(var i=0;i<arguments.length;i++){
				total += arguments[i];
			}
			return total;
		}
	}
	try{
		add("a","b");
	}catch(e){
		//TODO handle the exception
		console.log(e.name+":"+e.message);
	}
	//关于函数原型继承
	Function.prototype.testMethod = function(){
		console.log("come in");
	}
	
	que.testMethod();
	
	//关于递归（操作dom树形）
	var walk_the_dom = function walk(node,func){
		func(node);
		node= node.firstChild;
		while(node){
			walk(node,func);
			node = node.nextSibling;
		}
	}
	
	//关于replace
	String.prototype.deentity = function(){
		var entity = {
			"quot":'"',
			"lt":"<",
			"gt":">"
		};
		return this.replace(/&([^&;]+);/g, function(a,b){
			var r = entity[b];
			return typeof r === "string" ? r:a;
		});
	}
	
	var str = "jsjjs&lt;sjjdlkls&gt;".deentity()
	console.log(str);
	
	//关于模块化
	
	var serial_maker = function(){};
	
	//关于函数级联
	HTMLElement.prototype.setHeight = function(){
		this.style.height="100px";
		return this;
	}
	HTMLElement.prototype.setWidth = function(){
		this.style.width = "100px";
		return this;
	}
	HTMLElement.prototype.setBag = function(){
		this.style.backgroundColor="blue";
		return this;
	}
	var getElement = function(id){
		return document.getElementById(id);
	}
	getElement("cascade").setWidth().setHeight().setBag();
	
	//关于函数套用(实现参数不断增加，可应用于连加连减，连乘连除等操作)
	Function.prototype.curry = function(){
		var slice = Array.prototype.slice,
		    args = slice.apply(arguments),
		    that = this;
		return function(){
			return that.apply(null,args.concat(slice.apply(arguments)));
		}
	}
	
	var add1 = add.curry(1);
	var result = add1.curry(7);
	console.log(result(2));
	
	//函数之记忆。//函数柯里化
	
	var memoizer = function(memo,fundamental){
		var shell = function(n){
			var result = memo[n];
			if(typeof result !== "number" ){
				result = fundamental(shell,n);
				memo[n] = result;
			}
			return result;
		};
		return shell;
	};
	
	var fibonacci = memoizer([0,1],function(shell,n){
		return shell(n-1)+shell(n-2);
	});
	console.log(fibonacci(10));
	
	//关于函数化
	var mammal = function(spec){
		var that = {};
		that.get_name = function(){
			return spec.name;
			
		};
		that.says = function(){
			return spec.saying || '';
		};
		return that;
	};
	var myMammal = mammal({name:"kaisela",saying:"So what?"});
	
	var cat = function(spec){
		spec.saying = spec.saying || "";
		var that = mammal(spec);
		that.purr = function(n){
			var s = '';
			for(var i = 0;i < n;i++){
				if(s){
					s += '-';
				}
				s += 'r';
			}
			return s;
		}
		that.get_name = function(){
			return (spec.name|| '')+":cat";
		};
		
		return that;
	};
	
	
	var myCat = cat({
		name:"kaisela"
	});
	console.log(myCat.get_name());
	console.log(myCat.says());
	console.log(myMammal.get_name());
	
	Object.prototype.superior = function(name){
		var that = this,
			method = this[name];
		return function(){
			return method.apply(that,arguments);
		};
	};
	
	var coolcat = function(spec){
		var that = cat(spec),
		superior_get_name = that.superior("get_name");
		that.get_name = function(){
			return "like "+superior_get_name()+" baby!";
		}
		return that;
	};
	var myCoolcat = coolcat({name:"lily"});
	console.log(myCoolcat.get_name());
	
	//关于部件
	
	//关于正则
	var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
	var url = "http://qiji123.kerlai.net:81/GoodsBasic/Operate/12678?q#simen"
	var result = parse_url.exec(url);
	var names = ["url","scheme","slash","host","port","path","query","hash"];
	for(var i=0; i <names.length;i++){
		console.log(names[i]+":"+result[i]);
	}
	
	var test = {"name":"tets"}+"&ttt=1";
	console.log(test);
</script>